<template>
    <el-form :model="userinfo" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
            <el-input v-model="userinfo.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input v-model="userinfo.password"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="loginFun">登录</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
import {userRules} from '@/config/rules';
import {mapActions} from 'vuex';
export default {
    data(){
        return {
            userinfo:{
                username:'',
                password:''
            },
            rules:userRules
        }
    },
    methods:{
        ...mapActions('user',['login']),
        async loginFun(){
            //校验成功
            await this.$refs.ruleForm.validate();

            //登录
            let res = await this.login(this.userinfo);

            if(res.code === 1){
                this.$message('登录成功');
                this.$router.push('/main')
            }else{
                 this.$message('登录失败');
            }
        }
    }
}
</script>